package com.xhh.enjoyapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.xhh.enjoyapp.Bean.User;
import com.xhh.enjoyapp.Fragment.FragmentChat;
import com.xhh.enjoyapp.Fragment.FragmentHome;
import com.xhh.enjoyapp.Fragment.FragmentMine;
import com.xhh.enjoyapp.activity.Login;
import com.xhh.enjoyapp.adapter.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;

public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener, ViewPager.OnPageChangeListener {

    private ViewPager viewPager;
    private BottomNavigationBar bottomNavigationBar;
    private List<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = findViewById(R.id.viewpager);
        bottomNavigationBar = findViewById(R.id.bottom);

        initView();

    }

    private void initView() {
        initViewPager();
        initBottomNavigationBar();

    }

    private void initBottomNavigationBar() {
        // 配置底部导航栏
        bottomNavigationBar.setTabSelectedListener(this);
        bottomNavigationBar.clearAll();
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_DEFAULT);
        bottomNavigationBar.setBarBackgroundColor(R.color.white)
                            .setActiveColor(R.color.colorBase1)
                            .setInActiveColor(R.color.black);
        bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.homepage_fill,"首页").setInactiveIconResource(R.drawable.homepage))
                .addItem(new BottomNavigationItem(R.drawable.mobilephone_fill,"论坛").setInactiveIconResource(R.drawable.mobilephone))
                .addItem(new BottomNavigationItem(R.drawable.mine_fill,"我的").setInactiveIconResource(R.drawable.mine))
                .setFirstSelectedPosition(0)
                .initialise();


    }

    private void initViewPager() {
        // 配置ViewPager
        viewPager.setOffscreenPageLimit(3);

        fragments = new ArrayList<Fragment>();
        fragments.add(new FragmentHome());
        fragments.add(new FragmentChat());
        fragments.add(new FragmentMine());

        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(),0,fragments));
        viewPager.addOnPageChangeListener(this);
        viewPager.setCurrentItem(0);

    }

    @Override
    public void onTabSelected(int position) {
        viewPager.setCurrentItem(position);
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        bottomNavigationBar.selectTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


//    username = findViewById(R.id.tv_username);
//    nickname = findViewById(R.id.tv_nickname);
//
//    BmobUser bmobUser = BmobUser.getCurrentUser(User.class);
//
//    // Bmob查询用户
//    String objectId = bmobUser.getObjectId();
//    BmobQuery<User> bmobQuery = new BmobQuery<User>();
//        bmobQuery.getObject(objectId, new QueryListener<User>() {
//        @Override
//        public void done(User user, BmobException e) {
//            if(e == null){
//                username.setText(user.getUsername());
//                nickname.setText(user.getNickname());
//            } else {
//                Log.e("MainActivity", "查询失败:" + e);
//                Toast.makeText(MainActivity.this, "查询失败", Toast.LENGTH_LONG).show();
//            }
//        }
//    });
}
